package my.leetcode.solution;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 思路:
 */
public class P1 {

    public static int[] twoSum(int[] nums, int target) {
        int [] r = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0;i < nums.length;i++) {
            Integer t = map.get(target - nums[i]);
            if(t == null) {
                map.put(nums[i], i);
            } else {
                r[0] = t;
                r[1] = i;
                break;
            }
        }
        return r;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{2,7,11,15}, 9)));
    }

}
